package pnca;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.ma2.*;
import ucar.nc2.Attribute;
import ucar.nc2.Dimension;

/**
 * Test driver for reading NetCDF files.
 *
 * @author Brett Yasutake (Primary)
 * @author Niko Simonson (Contributing)
 */
public class NetCDFInterrogator {

    // Test output file name for file writing
    private static final String FILENAME = "/home/brett/NetBeans Projects/pac-nw-climate-analysis/AnnualFrostDaysProvenance";

    /**
     * Main driver function.
     * 
     * @param args - N/A
     */
    public static void main(String args[]) {

//        Logger logger = Logger.getLogger(NetCDFInterrogator.class.getName());
//        
//       
//         FileHandler handler;
//        try {
//            handler = new FileHandler("Exceptions.log");
//            logger.addHandler(handler);
//        } catch (IOException | SecurityException ex) {
//            Logger.getLogger(NetCDFInterrogator.class.getName()).log(Level.SEVERE, null, ex);
//        }
//            
//        
//         logger.log(Level.SEVERE, "TEST MESSAGE", new Exception());
        
        // Test input file for reading
        NetcdfFile targetFile = null;

        try {
            // Open the file
            targetFile = NetcdfFile.open(FILENAME, null);              
            
//            List<Attribute> globals = targetFile.getGlobalAttributes();
//            Iterator<Attribute> giter = globals.iterator();
//            System.err.println("GLOBAL ATTRIBUTES:");
//            while(giter.hasNext()) {
//                Attribute gatt = giter.next();
//                System.err.println(gatt.toString());
//                
//            }
            
//            List<Dimension> dims = targetFile.getDimensions();
//            Iterator<Dimension> diter = dims.iterator();
//            System.err.println("DIMS");
//            Dimension currentDim;
//            while(diter.hasNext()) {
//                currentDim = diter.next();
//                System.err.println(currentDim.toString());
//            }
            
            
            
            // Get list of file's variables
            List<Variable> targetVariables = targetFile.getVariables();

            // Describe the file
//            System.out.println("Number of variables in file: " + targetVariables.size());
//            System.out.println();
//
//            System.out.println("getdetailinfo: " + targetFile.getDetailInfo());
//            System.out.println("filetypedesc " + targetFile.getFileTypeDescription());

             //Read each variable
            for (int i = 0; i < targetVariables.size(); i++) {
                // Get the current Variable
                Variable curr = targetVariables.get(i); 

                System.out.print("Index " + i + ": ");

//                // Number of values for the current variable
//                ArrayDouble.D2 d3values = (ArrayDouble.D2) curr.read();
//                System.out.println("number of values: " + d3values.getSize());
                
                ArrayChar.D2 d2values = (ArrayChar.D2)curr.read();
                System.out.println("number of values: " + d2values.getSize());
                
                IndexIterator iter = d2values.getIndexIterator();
                while (iter.hasNext()) {
                    iter.next();
                    System.out.println(iter.getCurrentCounter()[0]
                            + " : " + iter.getCharCurrent());
                }
            }
//
//                // iterate through all values
//                IndexIterator iter = d3values.getIndexIterator();
//                while (iter.hasNext()) {
//                    iter.next();
//                    System.out.println(iter.getCurrentCounter()[0]
//                            + ", " + iter.getCurrentCounter()[1]
//                            + " : " + iter.getDoubleCurrent());
//                }
//                
//                
//            }

            // Describe the global attributes
//            System.err.println(targetFile.getGlobalAttributes().toString());
//            System.err.println(targetFile.getTitle());
            
//                        Variable target = targetVariables.get(0);
//                        List<Attribute> list = target.getAttributes();
//                        System.err.println(list.size());
//                        
//                        
//                        Iterator<Attribute> iter = list.iterator();
//                        Attribute att;
//                        while (iter.hasNext()) {
//                            att = iter.next();
//                            System.err.println(att.getName() + ", " + att.getStringValue() + ", " + att.getDataType().toString());
//              
//                        }
//
//            System.err.println(target.getFullName() + ", " + target.getNameAndDimensions() + ", " + target.getDescription() + ", " + target.getDataType().toString());
//            ArrayChar values =  (ArrayChar)target.read();
//            System.err.println("Values: " + values);
        } 

        catch (java.io.IOException e) {
            e.printStackTrace();
        } 
        finally {
            // Close out the file no matter what occurs
            if (targetFile != null) {
                try {
                    targetFile.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }
}
